﻿using System.Data;

namespace NazTek.Extension.Clr4
{
    /// <summary>
    /// Extends <see cref="IDataReader"/>
    /// </summary>
    public static class DataReaderExtension
    {
        /// <summary>
        /// Gets the value of the specified field name
        /// </summary>
        /// <typeparam name="T">Type of the field value to get</typeparam>
        /// <param name="reader">To extract data from</param>
        /// <param name="name">Field name</param>
        /// <returns>Typed field value</returns>
        public static T GetTypedValue<T>(this IDataReader reader, string name)
        {
            Parameter.Validate("name", name); 
            object val = reader[name];
            return val == System.DBNull.Value ? default(T) : (T)val;
        }
    }
}
